package com.app.qtjk.lsh.service;
import java.util.ArrayList;
import java.util.HashMap;

import com.app.common.dataDictionary.DE.DE_BRANDID;
import com.app.common.dataDictionary.DE.DE_C_BRAND_CLICKNUMBER;
import com.app.common.dataDictionary.DE.DE_C_BRAND_TOTALWEALTH;
import com.app.common.dataDictionary.DE.DE_OPPTIME;
import com.app.common.dataDictionary.DE.DE_USERID;
import com.app.common.dataDictionary.DE.DE_USERWEALTH;
import com.app.common.dataDictionary.PO.BrandmaintainClicknumPO;
import com.app.common.dataDictionary.PO.BrandmaintainClicksPO;
import com.app.common.dataDictionary.PO.BrandmaintainWealthPO;
import com.app.common.dataDictionary.PO.TCategoryShopPO;
import com.app.common.dataDictionary.PO.TUserPO;
import com.app.common.utils.CollectionUtil;
import com.app.common.utils.DateFormatUtil;
import com.app.common.utils.FilterUtil;
import com.app.common.utils.ReqJsonUtil;
import com.app.common.utils.StringUtil;
import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.kernel.utils.SwordServiceUtils;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;

@ServiceContainer
public class BrandService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(BrandService.class);

	public BrandService() throws Exception {
		super();
	}


	/**
	 * 用户今天点击品牌是否增加财富值接口（连带增加品牌点击量功能）
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "BrandService_todaySfAddWealth")
	public IResData todaySfAddWealth(IReqData req) throws Exception {

		log.debug("开始进入 BrandService_todaySfAddWealth 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		dao.getTransactionService().setAutoCommit(false);
		IResData res = new SwordRes();
		String todaySfAddWealth = "false";
		try {
			SQLParam param = new SQLParam();
			// 获取前台post的数据
			String userId = ReqJsonUtil.getPostParam(req,"userId");
			String cBrandId = ReqJsonUtil.getPostParam(req,"cBrandId");
			param.addParam(userId);
			param.addParam(cBrandId);
			if (CollectionUtil.isNotBlankMap(dao.queryMapByKey("BrandService_todaySfAddWealth", param))) {
				todaySfAddWealth = "true";
			}
			if (CollectionUtil.isBlankMap(dao.queryMapByKey("BrandService_todaySfAddClicknum", param))) {
				//新增用户点击品牌增加点击量维护记录
				BrandmaintainClicknumPO po = new BrandmaintainClicknumPO();
				po.setUserid(new DE_USERID(userId));
				po.setBrandid(new DE_BRANDID(cBrandId));
				po.setOpptime(new DE_OPPTIME(DateFormatUtil.getNowDate()));
				int i = dao.insertPO(po);
				//给商户增加点击量
				param.clearParams();
				param.addParam(cBrandId);
				TCategoryShopPO shopPO = dao.queryPOByKey("GetShopPOById", param, TCategoryShopPO.class);
				shopPO.setCBrandClicknumber(new DE_C_BRAND_CLICKNUMBER(shopPO.getCBrandClicknumber().value+1));
				int j = dao.updatePO(shopPO);
				if (i>0 && j>0) {
					dao.commitAll();
				}
			}
		} catch (Exception e) {
			dao.rollbackAll();
			e.printStackTrace();
		}
		
		res.put("todaySfAddWealth", todaySfAddWealth);
		return res;
	}

	 		
	/**
	 * 用户点击品牌页底部按钮增加财富值
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Service(serviceName = "BrandService_addWealth")
	public IResData addWealth(IReqData req) throws Exception {

		log.debug("开始进入 BrandService_addWealth 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		dao.getTransactionService().setAutoCommit(false);
		IResData res = new SwordRes();
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();
		HashMap hashMap = new HashMap();
		String sfEnough = "false";
		String addWealthSuccess = "false"; 
		try {
			SQLParam param = new SQLParam();
			// 获取前台post的数据
			String userId = ReqJsonUtil.getPostParam(req,"userId");
			String cBrandId = ReqJsonUtil.getPostParam(req,"cBrandId");
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
			}else {
				hashMap.put("returnStatus", "true");
				//调用检查用户今天点击品牌是否已增加过财富值
				res = (IResData) SwordServiceUtils.callService("BrandService_todaySfAddWealth", req);
				hashMap.put("todaySfAddWealth", StringUtil.parse2String(res.get("todaySfAddWealth")));
				if ("false".equals(res.get("todaySfAddWealth"))) {
					param.addParam(cBrandId);
					TCategoryShopPO shopPO = dao.queryPOByKey("GetShopPOById", param, TCategoryShopPO.class);
					if (shopPO.getCBrandTotalwealth().value > shopPO.getCBrandWealth().value) {
						sfEnough = "true";
					}
					hashMap.put("sfEnough", sfEnough);
					if ("true".equals(sfEnough)) {
						//获得一个用户每天可点击次数
						int userBrandClicksInitValue = Integer.parseInt(StringUtil.parse2String(dao.queryColumnByKey("AppParamManageService_getParamValue", null, 1)));
						//判断用户今天点击品牌的次数是否已超
						param.clearParams();
						param.addParam(userId);
						int clicks = Integer.parseInt(StringUtil.parse2String(dao.queryColumnByKey("getUserClicksByUserid", param, 1)));
						if (clicks >= userBrandClicksInitValue) {
							hashMap.put("clicks", "false");
							hashMap.put("clicksValue", userBrandClicksInitValue);
							hashMap.put("addWealthSuccess", "true");
							hashMap.put("addWealthValue", 0);
						}else {
							hashMap.put("clicks", "true");
							hashMap.put("clicksValue", userBrandClicksInitValue);
							if (shopPO.getCBrandWealth().value > 0) {
								//增加品牌点击次数维护表
								BrandmaintainClicksPO clicksPO = new BrandmaintainClicksPO();
								clicksPO.setUserid(new DE_USERID(userId));
								clicksPO.setBrandid(new DE_BRANDID(cBrandId));
								clicksPO.setOpptime(new DE_OPPTIME(DateFormatUtil.getNowDate()));
								int m = dao.insertPO(clicksPO);
								//新增用户今天点击品牌增加财富值维护表记录
								BrandmaintainWealthPO po = new BrandmaintainWealthPO();
								po.setUserid(new DE_USERID(userId));
								po.setBrandid(new DE_BRANDID(cBrandId));
								po.setOpptime(new DE_OPPTIME(DateFormatUtil.getNowDate()));
								int k = dao.insertPO(po);
								//给商户减财富值
								shopPO.setCBrandTotalwealth(new DE_C_BRAND_TOTALWEALTH(shopPO.getCBrandTotalwealth().value-shopPO.getCBrandWealth().value));
								int i = dao.updatePO(shopPO);
								//给用户加财富值
								param.clearParams();
								param.addParam(userId);
								TUserPO userPO = dao.queryPOByKey("UserService_getUserPOByID", param, TUserPO.class);
								userPO.setUserwealth(new DE_USERWEALTH(userPO.getUserwealth().value+shopPO.getCBrandWealth().value));
								int j = dao.updatePO(userPO);
								if (i>0 && j>0 && k>0 && m>0) {
									dao.commitAll();
									addWealthSuccess = "true";
								}
							}
							
							hashMap.put("addWealthSuccess", addWealthSuccess);
							if ("false".equals(addWealthSuccess)) {
								hashMap.put("addWealthValue", 0);
							}else {
								hashMap.put("addWealthValue", shopPO.getCBrandWealth().value);
							}
						}
					}
				}
			}	
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;
	}
	
	/**
	 * 给对应的品牌应用增加下载次数
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "BrandService_addAppCs")
	public IResData addAppCs(IReqData req) throws Exception{

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		log.debug("开始进入 BrandService_addAppCs 方法。");
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();		
		HashMap hashMap = new HashMap();
		IResData res = new SwordRes();
		try {
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
			}else {
				hashMap.put("returnStatus", "true");
				String phoneType = ReqJsonUtil.getPostParam(req,"phonetype");
				String cBrandId = ReqJsonUtil.getPostParam(req, "cBrandId");
				SQLParam param = new SQLParam();
				param.addParam(cBrandId);
				int j = 0;
				if ("ios".equals(phoneType)) {
					j = dao.executeByKey("BrandService_updateIosCs", param);
				}
				if ("android".equals(phoneType)) {
					j = dao.executeByKey("BrandService_updateAndroidCs", param);
				}
				if (j > 0) {
					hashMap.put("success", "true");
				}else {
					hashMap.put("success", "false");
				}
			}
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;
	}

}